/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package sza.oracle.apps.ap.oie.receive.webui;

import java.io.Serializable;

import kma.oracle.apps.ap.bank.server.PaymentVOImpl;

import oracle.apps.fnd.common.MessageToken;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OADialogPage;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import oracle.apps.fnd.framework.webui.beans.message.OAMessageCheckBoxBean;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;

/**
 * Controller for ...
 */
public class BatchReceiveCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
        
  String statusInput="";
  String submitPersonId ="";
  String submitDateInput ="";
  String submitDateTo ="";
  String reportHeaderIdInput ="";
  String costCenterInput = "";
    String receiptStatus = "";

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
     // System.err.println("selectCount"+selectCount);
      String javaScript="JAVASCRIP:function getSelectedCount(){\n" + 
      "        var   ingc; \n" + 
      "        var selectCount = 0;\n" + 
      "        var total = 0;\n" + 
      "        var curElm;\n" + 
      "        var nextElm;\n" + 
      "        for   (ingc=0;ingc <document.all.length;ingc++) \n" + 
      "        { \n" + 
      "            if   (document.all(ingc).type=='checkbox')   \n" + 
      "            { \n" + 
      "               curElm = document.all(ingc);" + 
      "               if(curElm.checked){\n" + 
      "                  selectCount = selectCount+1;\n" + 
      "                  nextElm = curElm.parentNode.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.firstChild;\n" + 
      "                  total = total+(nextElm.innerText*1.0);\n"+
      "               }\n" + 
      "            } \n" + 
      "        } \n" + 
      "        document.getElementById('region176').innerText = selectCount;\n" + 
      "        document.getElementById('region1813').innerText = total.toFixed(2);\n" + 
      "       \n" + 
      "\n" + 
      "}";
      pageContext.putJavaScriptFunction("getSelectedCount",javaScript);     

      OAMessageCheckBoxBean checkBox=(OAMessageCheckBoxBean)webBean.findChildRecursive("SelectBox"); 
      System.err.println("checkBox.getID()"+checkBox.getID());
      checkBox.setOnClick("javascript:getSelectedCount()"); 
  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    OAApplicationModule am = pageContext.getApplicationModule(webBean);
    if(pageContext.getParameter("GetRecBtn")!=null){
        query(pageContext,am);
         
    } else if(pageContext.getParameter("ReceiveBtn")!=null){
            OAViewObject vo = (OAViewObject)am.findViewObject("BatchReceiveVO1");
            if(vo.getFetchedRowCount()<=0)
            {
              throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
            }
            Row[] selectedRows = vo.getFilteredRowsInFetchedRange("Selected", "Y");
            if(selectedRows.length <=0 )
            {
              throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
            }
             if(selectedRows.length <= 0){
                 throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
             } else {
                 MessageToken[] tokens = { new MessageToken("SELECTED_COUNT", selectedRows.length+"")};
                 OAException mainMessage = new OAException("SQLSZA", "SZA_OIE_CONFIRM_RECEIVE", tokens);
                 OADialogPage dialogPage = new OADialogPage(OAException.WARNING, mainMessage, null, "", "");
                 String yes = pageContext.getMessage("SQLSZA", "SZA_OIE_CONFIRM", null);
                 String no = pageContext.getMessage("SQLSZA", "KMA_CANCEL_BTN", null);
                 dialogPage.setOkButtonItemName("ReceiveYesBtn");
                 dialogPage.setOkButtonToPost(true);
                 dialogPage.setNoButtonToPost(true);
                 dialogPage.setPostToCallingPage(true);
                 dialogPage.setOkButtonLabel(yes); 
                 dialogPage.setNoButtonLabel(no);
                 pageContext.redirectToDialogPage(dialogPage);
             }
      } else if(pageContext.getParameter("ReceiveYesBtn")!=null)
      {
        Serializable[] parameters = { "RECEIPT" };
        String count = (String)am.invokeMethod("ReceiveConfirm",parameters);
        Serializable[] parameter = { statusInput,submitPersonId, submitDateInput,submitDateTo,"@@@",costCenterInput,receiptStatus
                                         };
        am.invokeMethod("getReceiveRecord",parameter);
        MessageToken[] tokens = 
        { new MessageToken("RECEIVE_COUNT",  count) };
        throw new OAException("SQLSZA", 
                              "SZA_OIE_RECEIVE_SUCCESS", 
                              tokens,OAException.INFORMATION,null);
       
      } else if(pageContext.getParameter("ReturnBtn")!=null){
            OAViewObject vo = (OAViewObject)am.findViewObject("BatchReceiveVO1");
            if(vo.getFetchedRowCount()<=0)
            {
              throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
            }
            Row[] selectedRows = vo.getFilteredRowsInFetchedRange("Selected", "Y");
            if(selectedRows.length <=0 )
            {
              throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
            }
             if(selectedRows.length <= 0){
                 throw new OAException("SQLSZA", "KMA_MUST_SELECT_ONE_ITEM");
             } else {
                 for(int i=0;i<selectedRows.length;i++)
                 {
                   String comment = (String)selectedRows[i].getAttribute("ReceiptComment");
                   if(comment==null || "".equals(comment))
                   {
                     throw new OAException("SQLSZA","SZA_OIE_MUST_COMMENT");
                   }
                 }
                 
                 MessageToken[] tokens = { new MessageToken("SELECTED_COUNT", selectedRows.length+"")};
                 OAException mainMessage = new OAException("SQLSZA", "SZA_OIE_CONFIRM_RETURN", tokens);
                 OADialogPage dialogPage = new OADialogPage(OAException.WARNING, mainMessage, null, "", "");
                 String yes = pageContext.getMessage("SQLSZA", "SZA_OIE_CONFIRM", null);
                 String no = pageContext.getMessage("SQLSZA", "KMA_CANCEL_BTN", null);
                 dialogPage.setOkButtonItemName("ReturnYesBtn");
                 dialogPage.setOkButtonToPost(true);
                 dialogPage.setNoButtonToPost(true);
                 dialogPage.setPostToCallingPage(true);
                 dialogPage.setOkButtonLabel(yes); 
                 dialogPage.setNoButtonLabel(no);
                 pageContext.redirectToDialogPage(dialogPage);
             }
      }else if(pageContext.getParameter("ReturnYesBtn")!=null)
      {
        Serializable[] parameters = { "RETURN" };
        String count = (String)am.invokeMethod("ReceiveConfirm",parameters);
        Serializable[] parameter = { statusInput,submitPersonId, submitDateInput,submitDateTo,"@@@",costCenterInput
                                         };
        am.invokeMethod("getReceiveRecord",parameter);
        MessageToken[] tokens = 
        { new MessageToken("RETURN_COUNT",  count) };
        throw new OAException("SQLSZA", 
                              "SZA_OIE_RETURN_SUCCESS", 
                              tokens,OAException.INFORMATION,null);
       
      }
  }
  
  private void query(OAPageContext pageContext,OAApplicationModule am)
  {
    statusInput = pageContext.getParameter("StatusInput");
    submitPersonId = pageContext.getParameter("SubmitPersonId");
    submitDateInput = pageContext.getParameter("SubmitDateInput");
    submitDateTo = pageContext.getParameter("SubmitDateTo");
    reportHeaderIdInput = pageContext.getParameter("ReportHeaderIdInput");
    costCenterInput = pageContext.getParameter("CostCenter");
    receiptStatus = pageContext.getParameter("ReceiptStatusInput");
    Serializable[] parameters = { statusInput,submitPersonId, submitDateInput,submitDateTo,reportHeaderIdInput,costCenterInput,receiptStatus
                                     };
    am.invokeMethod("getReceiveRecord",parameters);
  }

}
